Conditions | 1 |
Paths | 2 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
15 | function upgrades(state, visibility, upgrade, data) { |
||
16 | let ct = this; |
||
17 | ct.state = state; |
||
18 | ct.data = data; |
||
19 | let sortFunc = upgrade.sortFunctions(data.upgrades); |
||
20 | |||
21 | // tries to buy all the upgrades it can, starting from the cheapest |
||
22 | ct.buyAll = function (slot) { |
||
23 | let currency = data.elements[slot.element].main; |
||
24 | let cheapest; |
||
25 | let cheapestPrice; |
||
26 | do{ |
||
27 | cheapest = null; |
||
28 | cheapestPrice = Infinity; |
||
|
|||
29 | for(let up of ct.visibleUpgrades(slot, data.upgrades)){ |
||
30 | let price = data.upgrades[up].price; |
||
31 | if(!slot.upgrades[up] && |
||
32 | price <= state.player.resources[currency].number){ |
||
33 | if(price < cheapestPrice){ |
||
34 | cheapest = up; |
||
35 | cheapestPrice = price; |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | if(cheapest){ |
||
40 | upgrade.buyUpgrade(state.player, |
||
41 | slot.upgrades, |
||
42 | data.upgrades[cheapest], |
||
43 | cheapest, |
||
44 | cheapestPrice, |
||
45 | currency); |
||
46 | } |
||
47 | }while(cheapest); |
||
48 | }; |
||
49 | |||
50 | ct.buyUpgrade = function (name, slot) { |
||
51 | let price = data.upgrades[name].price; |
||
52 | let currency = data.elements[slot.element].main; |
||
53 | upgrade.buyUpgrade(state.player, |
||
54 | slot.upgrades, |
||
55 | data.upgrades[name], |
||
56 | name, |
||
57 | price, |
||
58 | currency); |
||
59 | }; |
||
60 | |||
61 | ct.visibleUpgrades = function(slot) { |
||
62 | return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[state.player.options.sortIndex]); |
||
63 | }; |
||
64 | |||
65 | function isBasicUpgradeVisible(name, slot) { |
||
66 | let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name]); |
||
67 | return isVisible && (!state.player.options.hideBought || !slot.upgrades[name]); |
||
68 | } |
||
69 | } |
||
70 |